package com.cndcare.msc.netty.rocketmq.reveiver;

import cn.hutool.core.bean.BeanUtil;
import com.cndcare.msc.netty.bean.BindAlarmBean;
import com.cndcare.msc.netty.dao.jpa.BindAlarmBeanJpa;
import com.cndcare.msc.netty.rocketmq.client.StreamClient;
import com.cndcare.msc.netty.rocketmq.dto.BindAlarmMqDto;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.stream.annotation.EnableBinding;
import org.springframework.cloud.stream.annotation.StreamListener;
import org.springframework.stereotype.Component;

import java.util.Date;

@Component
@EnableBinding(StreamClient.class)
@Slf4j
public class StreamReceiver {

    @Autowired
    private BindAlarmBeanJpa bindAlarmBeanJpa;

    @StreamListener(StreamClient.BIND_ALARM_MQ_NETTY_ALARM)
    public void getBindAlarmMq(BindAlarmMqDto bindAlarmMqDto) {
        log.info("bindAlarmMqDto = {}", bindAlarmMqDto);

        BindAlarmBean bindAlarmBean = bindAlarmBeanJpa.findByAppIdAndOpenId(
                bindAlarmMqDto.getAppId(), bindAlarmMqDto.getOpenId());
        if (null == bindAlarmBean) {
            bindAlarmBean = new BindAlarmBean();
            BeanUtil.copyProperties(bindAlarmMqDto, bindAlarmBean, "id");
            bindAlarmBean.setCreateTime(new Date());
            bindAlarmBeanJpa.save(bindAlarmBean);
        }

    }
}
